package logic

import (
	"cart/model/mysql"
	"context"
	"gorm.io/gorm"
	"strconv"

	"cart/cart"
	"cart/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartListLogic {
	return &CartListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 购物车列表

func (l *CartListLogic) CartList(in *cart.CartListReq) (*cart.CartListResp, error) {
	c := mysql.Cart{Model: gorm.Model{ID: uint(in.UserId)}}
	list, err := c.CartList()
	if err != nil {
		logx.Error("列表查询失败")
		return nil, err
	}
	var carList []*cart.CartItem
	for _, v := range list {
		carList = append(carList, &cart.CartItem{
			GoodsId:    v.GoodsId,
			GoodsTitle: v.GoodsTitle,
			GoodsPrice: v.GoodsPrice,
			Num:        v.Num,
			UserId:     v.UserId,
			Checked:    strconv.Itoa(int(v.Checked)),
		})
	}
	return &cart.CartListResp{CartList: carList}, nil
}
